/**
 * [fcMessage]
 * @基于layui Notice
 * @ver: 1.1
 * @date: 2021-7-5
 * @author: cm.yu
 */
layui.link('./js/lay-module/fcMessage/fcMessage.css');
layui.define(['jquery'],function (exports) {
	var $ =  layui.$,
		noticeFlag = true;

	var _m = 'message-notice',
		_fadeOutClass = 'notice-slide-fadeOut',
		_fadeInClass = 'notice-slide-fadeIn';

    var obj = {
    	open: function (info,options) {
    		var defaults = {
                target: 'body',
				duration:2000,		//显示时间, 毫秒。设为 0 则不会自动关闭
				showClose: true,	//是否显示关闭按钮
				position:'center',	//显示位置默认居中
    		}
    		var opts = $.extend({},defaults,options);
    		
    		const icon = {
    			'info':'<svg t="1586399045233" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4625" width="15" height="15"><path d="M522.318769 273.329231a58.919385 58.919385 0 1 0 58.919385-58.604308 57.186462 57.186462 0 0 0-58.919385 58.604308zM391.325538 476.790154a280.339692 280.339692 0 0 0 0 34.422154l51.357539-61.833846a40.014769 40.014769 0 0 1 29.223385-17.565539 13.312 13.312 0 0 1 7.876923 16.462769l-85.07077 281.757539a56.792615 56.792615 0 0 0 53.878154 72.625231 176.443077 176.443077 0 0 0 144.068923-102.4 277.740308 277.740308 0 0 0 0.945231-35.524924l-51.436308 61.912616a41.275077 41.275077 0 0 1-30.168615 17.565538 13.154462 13.154462 0 0 1-7.876923-14.887384l85.622154-283.175385a57.816615 57.816615 0 0 0-53.090462-72.861538 219.293538 219.293538 0 0 0-145.329231 103.502769zM512.393846 0a512 512 0 1 1-512 512 512 512 0 0 1 512-512z" fill="#ffffff" p-id="4626"></path></svg>',
    			'success':'<svg t="1586399279307" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5668" width="15" height="15"><path d="M512 1024C229.2352 1024 0 794.7648 0 512S229.2352 0 512 0s512 229.2352 512 512-229.2352 512-512 512z m271.5264-653.7088l-54.2976-54.3104-307.7376 307.7376-126.72-126.72-54.2976 54.3104L385.28 696.128l36.1984 36.1984z" fill="#ffffff" p-id="5669"></path></svg>',
    			'error':'<svg t="1586398977558" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="3454" width="15" height="15"><path d="M512.345 2.207c-281.254 0-509.254 228-509.254 509.254s228 509.254 509.254 509.254 509.253-228 509.253-509.253-228-509.255-509.253-509.255z m223.847 663.93c19.628 19.672 19.628 51.58 0 71.252-9.813 9.836-22.676 14.76-35.538 14.76-12.861 0-25.724-4.925-35.539-14.76l-153.14-153.644-153.14 153.642c-9.814 9.836-22.677 14.76-35.532 14.76-12.869 0-25.724-4.925-35.538-14.76-19.628-19.672-19.628-51.58 0-71.253l153.14-153.648-153.141-153.641c-19.628-19.672-19.628-51.58 0-71.253 19.628-19.665 51.442-19.665 71.07 0l153.14 153.642 153.141-153.642c19.635-19.665 51.442-19.665 71.078 0 19.628 19.672 19.628 51.58 0 71.253L583.051 512.486l153.141 153.65z" fill="#ffffff" p-id="3455"></path></svg>',
    			'warning':'<svg t="1586399013003" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4345" width="16" height="16"><path d="M0 0h1024v1024H0z" fill="#F8F8F8" fill-opacity="0" p-id="4346"></path><path d="M512 51.2a460.8 460.8 0 1 1 0 921.6 460.8 460.8 0 0 1 0-921.6zM438.272 281.0368l9.8304 246.3744c1.3824 34.304 29.5936 61.3888 63.8976 61.3888s62.464-27.136 63.744-61.44l9.216-246.272A73.5232 73.5232 0 0 0 511.488 204.8a73.3184 73.3184 0 0 0-73.216 76.2368zM435.2 741.888v0.512A76.8 76.8 0 0 0 512 819.2a76.288 76.288 0 0 0 76.288-76.8v-0.512A76.8 76.8 0 0 0 511.488 665.6a76.288 76.288 0 0 0-76.288 76.288z" fill="#ffffff" p-id="4347"></path></svg>',
    			'close':'<svg t="1618303899071" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2025" width="18" height="18"><path d="M810.666667 273.493333L750.506667 213.333333 512 451.84 273.493333 213.333333 213.333333 273.493333 451.84 512 213.333333 750.506667 273.493333 810.666667 512 572.16 750.506667 810.666667 810.666667 750.506667 572.16 512z" p-id="2026" fill="#ffffff"></path></svg>',
    			'loading':'<svg viewBox="25 25 50 50" class="circular"><circle cx="50" cy="50" r="20" fill="none" class="path"></circle></svg>'
    		}
        	if(!noticeFlag) return false;
        	noticeFlag = false;

        	var close = '<em>'+icon.close+'</em>';
			$(opts.target).append('<div class="'+_m+' '+_fadeInClass+' '+opts.type+'" position="'+opts.position+'" '+(opts.offset?"offset="+opts.offset:"")+' '+(opts.target!="body"?"style='position:absolute;'":"")+'><p><i>'+icon[opts.type]+'</i>'+ info + (opts.showClose?close:"")+'</p></div>');
            if(opts.cancel) {
                this.cancel(opts.cancel);
            }else{
                this.listen(opts.duration,opts.onClose);
            }
    	},
    	listen: function (duration,cb) {
    		if(duration > 0){
				var timer = setTimeout(function(){
					obj.close(cb);
				},duration);
    		}
    		$(document).off('.'+_m+' p em').on('click','.'+_m+' p em',function(){
    			obj.close(cb);
    			clearTimeout(timer)
    		})
    	},
        close:function(cb) {
			$('.'+_m).addClass(_fadeOutClass);
			$('.'+_m).removeClass(_fadeInClass);
			 setTimeout(function(){
				$('.'+_m).remove();
				noticeFlag = true;
				if(cb) return cb();
			},300)
        },
        cancel:function(cb) {
            return cb();
        },
        info:function(info,opts) {
        	opts = $.extend(true,opts,{type:'info'})
        	this.open(info,opts);
        },
        success:function(info,opts) {
        	opts = $.extend(true,opts,{type:'success'})
        	this.open(info,opts);
        },
        warning:function(info,opts) {
        	opts = $.extend(true,opts,{type:'warning'})
        	this.open(info,opts);
        },
        error:function(info,opts) {
        	opts = $.extend(true,opts,{type:'error'})
        	this.open(info,opts);
        },
        loading:function(info,opts) {
        	opts = $.extend(true,opts,{type:'loading'})
        	this.open(info,opts);
        }
    };
	exports("fcMessage", obj);
});